<?php
/**
 *
 **/
class AttachmentsController extends AppController
{
	var $name='Attachments';

	function download($filename){
		$this->autoLayout=false;
		if (!$filename) {
			die('missing key');
		}else {
			$attachinfo=$this->Attachment->findByFilename($filename);
			if (!$attachinfo) {
				die('invalid key');
			}else {
				$attachinfo=$attachinfo['Attachment'];
				$filepath=WWW_ROOT.'attachments'.DS.$attachinfo['filename'];
				if(!file_exists($filepath)){
					die('missing file');
				}
				$this->set('attachinfo',$attachinfo);
				$this->set('filepath',$filepath);
			}
		}
	}

	function upload(){
		$this->autoLayout=false;
		$return=array();
		if ($this->data['Attachment']) {
			if(copy($this->data['Attachment']['file']['tmp_name'],WWW_ROOT.'attachments'.DS.$this->data['Attachment']['file']['name'])){
				if($this->Attachment->save(array(
					'Attachment'=>array(
						'filename'=>$this->data['Attachment']['file']['name'],
						'filetype'=>$this->data['Attachment']['file']['type'],
						'filesize'=>$this->data['Attachment']['file']['size'],
						'dateline'=>time()
					))
				)){
					$return['state']='1';
					echo json_encode($return);
					exit();
				}
			};
			$return['state']='0';
			echo json_encode($return);
			exit();
		}
	}
}
?>
